package kr.couponplay.android;

import java.util.Date;
import java.util.List;
import java.util.Map;

import kr.couponplay.android.domain.deal.Provider;
import kr.couponplay.android.support.AppUtils;
import kr.couponplay.android.support.CategoryUtils;
import kr.couponplay.android.support.ProviderUtils;
import kr.couponplay.android.support.StringUtils;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class AppContext {
	private static String sid;
	private static String newAppVersion;
	private static int dealListLimitCount;
	private static int dealTotalCount;
	private static long lastUpdatedTime;
	private static List<Category> mainCategories;
	private static Map<Integer, List<Category>> subCategories;
	private static List<Provider> providers;
	private static String fileCacheRootPath;
	private static int fileCacheCount;

	public static boolean isClearCategoryDeals = false;
	public static boolean isClearNewDeals = false;
	public static boolean isClearEndDeals = false;
	public static boolean isClearFreeDeals = false;
	public static boolean isClearSearchDeals = false;
	
	public static List<Category> getMainCategories(Context context) {
		if (context != null && mainCategories == null) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			CategoryUtils.setting(preferences.getString(Constants.KEY_CATEGORY_JSON, ""));
		}
		return mainCategories;
	}

	public static Map<Integer, List<Category>> getSubCategories(Context context) {
		if (context != null && subCategories == null) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			CategoryUtils.setting(preferences.getString(Constants.KEY_CATEGORY_JSON, ""));
		}
		return subCategories;
	}

	public static void setSubCategories(Map<Integer, List<Category>> subCategories) {
		AppContext.subCategories = subCategories;
	}

	public static void setMainCategories(List<Category> mainCategories) {
		AppContext.mainCategories = mainCategories;
	}

	public static String getSid(Context context) {
		if (context != null && StringUtils.isEmpty(sid)) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			sid = preferences.getString(Constants.KEY_SID, Constants.DEFAULT_SID);
		}
		return sid;
	}

	public static void setSid(String sid) {
		AppContext.sid = sid;
	}

	public static String getAppVersion(Context context) {
		if (context != null && StringUtils.isEmpty(newAppVersion)) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			newAppVersion = preferences.getString(Constants.NEW_APP_VERSION, AppUtils.getAppVersion(context));
		}		
		return newAppVersion;
	}

	public static void setNewAppVersion(String appVersion) {
		AppContext.newAppVersion = appVersion;
	}

	public static int getDealListLimitCount(Context context) {
		if (context != null && dealListLimitCount == 0) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			dealListLimitCount = preferences.getInt(Constants.KEY_DEAL_LIMIT_COUNT, Constants.DEFAULT_DEAL_LIMIT_COUNT);
		}
		return dealListLimitCount;
	}

	public static void setDealListLimitCount(int dealListLimitCount) {
		AppContext.dealListLimitCount = dealListLimitCount;
	}

	public static int getDealTotalCount(Context context) {
		if (context != null && dealTotalCount == 0) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			dealTotalCount = preferences.getInt(Constants.KEY_DEAL_TOTAL_COUNT, Constants.DEFAULT_DEAL_TOTAL_COUNT);
		}
		return dealTotalCount;
	}

	public static void setDealTotalCount(int dealTotalCount) {
		AppContext.dealTotalCount = dealTotalCount;
	}

	public static long getLastUpdatedTime(Context context) {
		if (context != null && lastUpdatedTime == 0) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			lastUpdatedTime = preferences.getLong(Constants.KEY_LAST_UPDATE_TIME, new Date().getTime());
		}		
		return lastUpdatedTime;
	}

	public static void setLastUpdatedTime(long lastUpdatedTime) {
		AppContext.lastUpdatedTime = lastUpdatedTime;
	}

	public static String getFileCacheRootPath(Context context) {
		if (context != null && StringUtils.isEmpty(fileCacheRootPath)) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			fileCacheRootPath = preferences.getString(Constants.KEY_FILE_CACHE_ROOT_PATH, "");
		}
		return fileCacheRootPath;
	}

	public static void setFileCacheRootPath(String fileCacheDir) {
		AppContext.fileCacheRootPath = fileCacheDir;
	}

	public static void setProviders(List<Provider> providers) {
		AppContext.providers = providers;
	}

	public static List<Provider> getProviders(Context context) {
		if (context != null && providers == null) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			ProviderUtils.setting(preferences.getString(Constants.KEY_PROVIDER_JSON, ""));
		}
		return providers;
	}

	public static Provider getProvider(Context context, String providerCode) {
		if (context != null && providers == null) {
			SharedPreferences preferences = context.getSharedPreferences(Constants.PREFERENCE_NAME, Activity.MODE_PRIVATE);
			ProviderUtils.setting(preferences.getString(Constants.KEY_PROVIDER_JSON, ""));
		}
		
		for (Provider provider : providers) {
			if (StringUtils.trimToEmpty(providerCode).equals(provider.getCode())) {
				return provider;
			}
		}
		return null;
	}

	public static int getFileCacheCount() {
		return fileCacheCount;
	}

	public static void setFileCacheCount(int fileCacheCount) {
		AppContext.fileCacheCount = fileCacheCount;
	}
}
